package br.urca.impl;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import br.urca.dao.FuncionarioDAO;
import br.urca.model.Funcionario;

@Transactional
public class JPAFuncionarioDAO implements FuncionarioDAO {
	
	private EntityManager manager;
	
	public JPAFuncionarioDAO() {
		this(null);
	}
	
	@Inject
	public JPAFuncionarioDAO(EntityManager manager) {
		this.manager = manager;
	}

	@Override
	public List<Funcionario> todos() {
		try {
			return this.manager.createQuery("SELECT f FROM Funcionario f", Funcionario.class)
					.getResultList();
		} catch(Exception e) {
			return null;
		}
	}

	@Override
	public Funcionario buscarPorMatricula(String matricula) {
		try {
			return this.manager.createQuery("SELECT f FROM Funcionario f "
					+ "WHERE f.matricula = :matricula ", Funcionario.class)
					.setParameter("matricula", matricula)
					.getSingleResult();
		} catch(Exception e) {
			return null;
		}
	}
	
	@Override
	public Funcionario buscarPorMatriculaSenha(String matricula, String senha) {
		try {
			return this.manager.createQuery("SELECT f FROM Funcionario f "
					+ "WHERE f.matricula = :matricula "
					+ "AND f.senha_md5 = :senha ", Funcionario.class)
					.setParameter("matricula", matricula)
					.setParameter("senha", senha)
					.getSingleResult();
		} catch(Exception e) {
			return null;
		}
	}

}
